## generate the country search url
## linqing.wlq@alibaba-inc.com
#! /usr/bin/perl/ -w
use strict;
use warnings;

my $SUFFIX=".urls";

if(@ARGV<=0){
	die("Please input a file with includes some keywords");
}
my $url_head="http://www.alibaba.com/countrysearch/";
my $url_suffix=".html";

my @magellanCountry=('ZA', 'MY', 'PH', 'PI');
my @searchwebCountry=('IN', 'US', 'UK', 'HK');

my $IN_FILE = $ARGV[0];
my $OUT_FILE_MAGELLAN=$IN_FILE.$SUFFIX.".magellan";
my $OUT_FILE_SEARCHWEB=$IN_FILE.$SUFFIX.".searchweb";


my $IN_FILE_HANDLER;
my $OUT_MAGELLAN;
my $OUT_SEARCHWEB;
open(IN_FILE_HANDLER, "<$IN_FILE")||die("Can not open file: $IN_FILE\n");
open(OUT_MAGELLAN, ">$OUT_FILE_MAGELLAN")||die("Can not open file: $OUT_FILE_MAGELLAN=\n");
open(OUT_SEARCHWEB, ">$OUT_FILE_SEARCHWEB")||die("Can not open file: $OUT_FILE_SEARCHWEB\n");

my $line;
while($line=<IN_FILE_HANDLER>){
	#remove the \r and \n
	chomp($line);
	if(length($line)<=0){
		next;
	}
	my $word = lc($line);
	$word =~ s/\s+/-/g;
	for(my $i=0; $i<scalar(@magellanCountry); $i++){
		my $magellanUrl = $url_head.$magellanCountry[$i]."/".$word.$url_suffix;
		print OUT_MAGELLAN $magellanUrl."\n";
	}
	for(my $i=0; $i<scalar(@searchwebCountry); $i++){
		my $searchwebUrl = $url_head.$searchwebCountry[$i]."/".$word.$url_suffix;
		print OUT_SEARCHWEB $searchwebUrl."\n";
	}
}
close OUT_MAGELLAN;
close OUT_SEARCHWEB;
close IN_FILE_HANDLER;
